
Public Class c_System_Error

    Public Shared enviar_correo As Boolean
    Public Shared StartupPath As String
    Public Shared Error_SMTP_var As String
    Public Shared Error_From_var As String
    Public Shared Error_To_var As String
    Public Shared Error_Subject_var As String

    Public Shared Function Send_Message(ByVal De As String, ByVal Para As String, ByVal Titulo As String, ByVal Mensaje As String) As Boolean
        Dim objEmail As New System.Net.Mail.MailMessage(De, Para)
        'objEmail.To = Error_To_var
        'objEmail.From = Error_From_var
        objEmail.Subject = Titulo
        objEmail.Body = Mensaje & Chr(13)
        objEmail.Body += GetData()
        objEmail.Priority = System.Net.Mail.MailPriority.High

        Dim serv As New System.Net.Mail.SmtpClient(Error_SMTP_var)
        Try
            If enviar_correo Then serv.Send(objEmail)
            Return True
        Catch exc As Exception
            Return False
        End Try
    End Function

    Public Shared Function Send_Error(ByVal ex As Exception) As Boolean
        If InStr(ex.Message, "Thread") = 0 Then
            Dim objEmail As New System.Net.Mail.MailMessage(Error_From_var, Error_To_var)
            'objEmail.To = New System.Net.Mail.MailAddress(Error_To_var)
            'objEmail.From = Error_From_var
            objEmail.Subject = Error_Subject_var ' & " - " & ID_Region & "/" & ID_Location
            objEmail.Body = "Message:" & ex.Message & vbCrLf
            objEmail.Body += "Source:" & ex.Source & vbCrLf
            objEmail.Body += "StackTrace:" & ex.StackTrace & vbCrLf
            objEmail.Body += GetData()
            objEmail.Priority = System.Net.Mail.MailPriority.High


            Dim serv As New System.Net.Mail.SmtpClient(Error_SMTP_var)
            'System.Net.Mail.SmtpMail.SmtpServer = Error_SMTP_var
            Try
                'System.Web.Mail.SmtpMail.Send(objEmail)
                If enviar_correo Then serv.Send(objEmail)
                Return True
            Catch exc As Exception
                Return False
            End Try
        End If
    End Function

    Public Shared Function Send_Error(ByVal Error_Description As String) As Boolean
        Dim objEmail As New System.Net.Mail.MailMessage(Error_From_var, Error_To_var)
        'objEmail.To = Error_To_var
        'objEmail.From = Error_From_var
        objEmail.Subject = Error_Subject_var
        objEmail.Body = Error_Description
        objEmail.Body += GetData()
        objEmail.Priority = System.Net.Mail.MailPriority.High

        Dim serv As New System.Net.Mail.SmtpClient(Error_SMTP_var)
        Try
            If enviar_correo Then serv.Send(objEmail)
            Return True
        Catch exc As Exception
            Return False
        End Try
    End Function

    Public Shared Function Send_Error(ByVal Error_Description As String, ByVal ex As Exception) As Boolean
        If InStr(ex.Message, "Thread") = 0 Then
            Dim objEmail As New System.Net.Mail.MailMessage(Error_From_var, Error_To_var)
            'objEmail.To = Error_To_var
            'objEmail.From = Error_From_var
            objEmail.Subject = Error_Subject_var
            objEmail.Body = "Message: " & ex.Message & vbCrLf
            objEmail.Body += "Source: " & ex.Source & vbCrLf
            objEmail.Body += "StackTrace: " & ex.StackTrace & vbCrLf
            objEmail.Body += "More: " & Error_Description
            objEmail.Body += GetData()
            objEmail.Priority = System.Net.Mail.MailPriority.High

            Dim serv As New System.Net.Mail.SmtpClient(Error_SMTP_var)
            Try
                If enviar_correo Then serv.Send(objEmail)
                'Threading.Thread.Sleep(1000)
                Return True
            Catch exc As Exception
                Return False
            End Try
        End If
    End Function

    Private Shared Function GetData() As String
        Dim Text As String = ""
        Text += vbCrLf
        Text += vbCrLf
        Text += "Data:" & vbCrLf
        Text += "|Aplication Path: " & StartupPath & vbCrLf
        Try
            Text += "|Arguments: " & Microsoft.VisualBasic.Command() & vbCrLf
        Catch ex As Exception
            Text += "|Arguments: Error..." & vbCrLf
        End Try
        Text += "|Machine Name: " & Environment.MachineName & " " & vbCrLf
        Text += "|OS Version: " & Environment.OSVersion.ToString & "    " & vbCrLf
        Text += "|User Name: " & Environment.UserName & " " & vbCrLf
        Return Text
    End Function
End Class
